#!/bin/sh -efux
# NB: /etc/initrd.mk carefully prepared by earlier scripts

fatal() { echo "** error: $@" >&1; exit 1; }

kver=
for KFLAVOUR in $GLOBAL_KFLAVOURS; do
	kver+=" $(rpm -qa 'kernel-image*' \
		--qf '%{version}-%{name}-%{release}\n' \
	| grep "$KFLAVOUR" \
	| sed 's/kernel-image-//')"
done

[ -n "$kver" ] ||
	fatal "no kernel version identified"


[ -s /.in/modules ] ||
	fatal "kernel modules set not found"

# copy initrd.mk to image
mkdir -p /.image/.disk
cp /etc/initrd.mk /.image/.disk
cd /.image

cat >> .disk/initrd.mk <<EOF
FEATURES += propagator
FEATURES += add-modules compress cleanup
EOF

for module in $(grep -v ^# /.in/modules | sort -u | tr -s '\n' ' '); do
	echo "MODULES_TRY_ADD += $module" >> .disk/initrd.mk
done

# FIXME: large storage systems can get that tmpfs filled up
# with debug data as of make-initrd 2.2.12
rm -vf  /usr/share/make-initrd/data/etc/udev/rules.d/00-debug.rules \
	/usr/share/make-initrd/data/lib/uevent/filters/debug

MAKE_INITRD_OPTS="--no-checks AUTODETECT= -c .disk/initrd.mk"
MAKE_INITRD_VER="`make-initrd -V \
	| sed -rn 's/^make-initrd version ([0-9.]+)/\1/p'`"

[ -z "$GLOBAL_VERBOSE" ] ||
	MAKE_INITRD_OPTS="$MAKE_INITRD_OPTS -v"

for KVER in $kver; do
	echo make-initrd $MAKE_INITRD_OPTS -k "$KVER" >> .disk/mkinitrd
	make-initrd $MAKE_INITRD_OPTS -k "$KVER" ||
		fatal "make-initrd failed"
done

cd /boot

case `arch` in
e2k)
	kname=image;;
*)
	kname=vmlinuz;;
esac

rm -f $kname initrd.img
ln -s $kname-$KVER $kname
ln -s initrd-$KVER.img initrd.img

:
